using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class TVC_CATE2DataAccess : Connection
    {
        #region Local Variable

        #endregion Local Variable

        #region Method
        public TVC_CATE2Info Get(string db, String maingroup, ref string sErr)
        {
            TVC_CATE2Info objEntr = new TVC_CATE2Info();
            InitConnect();
            InitCommand(@"SELECT     maingroup, mainname
                FROM         TVC_CATE2" + db + @"
                WHERE     (maingroup = @maingroup)");

            AddParameter(TVC_CATE2Info.Field.maingroup.ToString(), maingroup);

            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }

            if (list.Rows.Count > 0)
                objEntr = new TVC_CATE2Info(list.Rows[0]);
            //if (dr != null) list = CBO.FillCollection(dr, ref list);
            if (sErr != "") ErrorLog.SetLog(sErr);
            return objEntr;
        }

        public DataTable GetAll(string db,
        ref string sErr)
        {
            InitConnect();
            InitCommand(@"SELECT     maingroup, mainname
                FROM         TVC_CATE2" + db);
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }


            if (sErr != "") ErrorLog.SetLog(sErr);
            return list;
        }

        /// <summary>
        /// Return 1: Table is exist Identity Field
        /// Return 0: Table is not exist Identity Field
        /// Return -1: Erro
        /// </summary>
        /// <param name="tableName"></param>
        public Int32 Add(TVC_CATE2Info objEntr, ref string sErr)
        {
            int ret = -1;
            openConnection();
            InitCommand(@"INSERT INTO TVC_CATE2" + objEntr.db + @"
                      (maingroup, mainname)
                VALUES     (@maingroup,@mainname)");

            AddParameter(TVC_CATE2Info.Field.maingroup.ToString(), objEntr.maingroup);
            AddParameter(TVC_CATE2Info.Field.mainname.ToString(), objEntr.mainname);

            try
            {
                //command.ExecuteNonQuery();
                object tmp = executeScalar();
                if (tmp != null && tmp != DBNull.Value)
                    ret = Convert.ToInt32(tmp);
                else
                    ret = 0;
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);

            return ret;
        }

        public string Update(TVC_CATE2Info objEntr)
        {
            openConnection();
            InitCommand(@"UPDATE    TVC_CATE2" + objEntr.db + @"
                    SET mainname = @mainname
                WHERE     (maingroup = @maingroup)");
            AddParameter(TVC_CATE2Info.Field.maingroup.ToString(), objEntr.maingroup);
            AddParameter(TVC_CATE2Info.Field.mainname.ToString(), objEntr.mainname);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }

        public string Delete(string db,
        String maingroup
        )
        {
            openConnection();
            InitCommand(@"DELETE FROM TVC_CATE2" + db + @"
                WHERE     (maingroup = @maingroup)");
            AddParameter(TVC_CATE2Info.Field.maingroup.ToString(), maingroup);

            string sErr = "";
            try
            {
                executeNonQuery();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            return sErr;
        }


        public Boolean IsExist(string db,
        String maingroup
        )
        {
            InitConnect();
            InitCommand(@"SELECT  TOP(1)   maingroup
                FROM         TVC_CATE2" + db + @"
                WHERE     (maingroup = @maingroup)");

            AddParameter(TVC_CATE2Info.Field.maingroup.ToString(), maingroup);

            string sErr = "";
            DataTable list = new DataTable();
            try
            {
                list = executeSelect();
            }
            catch (Exception ex)
            {
                sErr = ex.Message;
            }
            finally {closeConnection();}
            if (sErr != "") ErrorLog.SetLog(sErr);
            if (list.Rows.Count == 1)
                return true;
            return false;
        }


        public DataTable GetTransferOut(string dtb, object from, object to, ref string sErr)
        {
            return GetAll(dtb, ref sErr);
        }
        #endregion Method

    }
}
